'use strict';

angular.module('SystemManage').controller('RoleController',
    ['$scope', '$stateParams', 'BaseModule', 'BaseFunction', 'BaseEnum', '$location', 'Authentication',
    function($scope, $stateParams, BaseModule, BaseFunction, BaseEnum, $location, Authentication) {
        //$scope.authentication = Authentication;
       
        $scope.btnSave = function(){
            if($scope.Module.Role.FocusedRecord.Code == undefined || 
                $scope.Module.Role.FocusedRecord.Code.trim() == ""){
                BaseFunction.alert("编码不能为空！", "保存错误");
                return;
            }
            if($scope.Module.Role.FocusedRecord.Name == undefined || 
                $scope.Module.Role.FocusedRecord.Name.trim() == ""){
                BaseFunction.alert("名称不能为空！", "保存错误");
                return;
            }

            var menus = [];
            for(var i=0; i<$scope.Module._Menu.Records.length; i++){
                if($scope.Module._Menu.Records[i].Menu == undefined || 
                    $scope.Module._Menu.Records[i].Menu == null){
                    BaseFunction.alert("菜单不能为空！", "保存错误");
                    return;
                }
                for(var j=0; j<menus.length; j++){
                    if(menus[j] == $scope.Module._Menu.Records[i].Menu._id){
                        BaseFunction.alert("菜单不能重复！", "保存错误");
                        return;
                    }
                }
                menus.push($scope.Module._Menu.Records[i].Menu);
            }

            $scope.Module.Role.FocusedRecord.Menus = menus;
            if($scope.Module.Role.FocusedRecord.RowStatus == BaseEnum.RowStatus.UnChange.Value){
                $scope.Module.Role.FocusedRecord.RowStatus = BaseEnum.RowStatus.Modify.Value;
            }
            $scope.Module.Role.save();
        };
        $scope.btnCancel = function(){
            $scope.Module.Role.cancel(displayMenus);            
        };
        $scope.btnAdd = function(){
            $scope.Module.Role.add();
            displayMenus();
        };
        $scope.btnDelete = function(){
            $scope.Module.Role.delete();
        };
        $scope.btnList = function(){
            $location.url('/index/Role/List');
        };
        $scope.btnFindChange = function(){
            displayMenus();
        };
        $scope.pageChange = function(){
            displayMenus();
        };
        $scope.refBeforeShow = function(refScope){
            var menus = [];
            for(var i=0; i<$scope.Module._Menu.Records.length; i++){
                if($scope.Module._Menu.Records[i].Menu != undefined && $scope.Module._Menu.Records[i].Menu != null){
                    menus.push($scope.Module._Menu.Records[i].Menu._id);
                }
            }
            refScope.Module.Condition = {Parent: {$ne: null}, _id: {$nin: menus}};
        }

        var displayMenus = function(){
        	$scope.Module._Menu.Records = [];
            if($scope.Module.Role.FocusedRecord.Menus != undefined && $scope.Module.Role.FocusedRecord.Menus != null){
                for(var i=0; i<$scope.Module.Role.FocusedRecord.Menus.length; i++){
                    var newRecord = $scope.Module._Menu.add();
                    newRecord.Menu = $scope.Module.Role.FocusedRecord.Menus[i];
                }
            }
            $scope.Module._Menu.emitOnLoadEvent();
        };

        //初始化
        BaseModule.initModule($scope, 'Role');
        $scope.Module.Role.Populate = {path: 'Menus', populate: {path: 'Parent', select: 'Name'}};
        $scope.Module.Role.AfterOnAdd = function(record){
            record.IsEffective = true;
        };

        BaseModule.initModule($scope, '_Menu');
        $scope.Module._Menu.PageSize = 10;

        if($stateParams.id) {
            $scope.Module.Role.findById($stateParams.id, function(record){
                $scope.Module.Role.FocusedRecord = record;
                displayMenus();
            });
        }
        else{
            $scope.btnAdd();
        };


        //下面代码是实现批量添加角色功能
        $scope.onBeforeShow = function(){
            $scope.Module.User.Condition = { Roles: {$ne: $scope.Module.Role.FocusedRecord._id} };  //不包含当前角色
            $scope.Module.User.load();
        };

        $scope.btnBatchOK = function(){
            var userIds  = new Array();
            var selectUsers = $scope.Module.User.SelectedRecords;
            if(selectUsers.length > 0){
                for(var i=0; i<selectUsers.length; i++) {
                    userIds.push(selectUsers[i]._id);
                }

                $scope.Module.User.updateByValue({_id: {$in: userIds}}, {$push: {"Roles": $scope.Module.Role.FocusedRecord._id}}, function(data){
                    BaseFunction.tip("批量分配角色完成");
                });
            }
            else{
                BaseFunction.alert("请选择用户后再分配");
                return false;
            }
        };

        BaseModule.initModule($scope, 'User');
        $scope.Module.User.Populate = {path: 'Department', select: 'Name'};
        $scope.Module.User.PageSize = 13;
    }
]);
